import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'home/home_menus.dart';
import 'home/home_banner.dart';
import 'home/fast_product.dart';
import 'home/home_title.dart';
import 'home/home_roll_news.dart';
import 'home/home_app_bar.dart';
import 'home/home_activity.dart';
import 'home/best_list.dart';
import 'home/new_product.dart';
import 'home/promotion_product.dart';
import '../utils/dio_util.dart';
import '../widget/my_dialog.dart';
import '../widget/error_page.dart';
import '../widget/empty_page.dart';

class HomePage extends StatefulWidget {
  HomePage({Key key}) : super(key: key);

  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage>
    with AutomaticKeepAliveClientMixin {
  bool isloaded = false;

  @override
  void initState() {
    super.initState();
    getHomeData(); //获取首页数据
  }

  Future<Map<String,dynamic>> getHomeData() async {
    String api = '/api/mall/public/index.service';
    DioUtil http = DioUtil();
    Map<String, dynamic> res = await http.get(api);
    print(res);
    return res;
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    ScreenUtil.init(context, width: 750, height: 1334);
    return Scaffold(
      appBar: HomeAppBar(),
      body:FutureBuilder<Map<String,dynamic>>(
        future: getHomeData(),
        builder: (context, AsyncSnapshot snapshot) {
          print(snapshot.data);
          switch (snapshot.connectionState) {
            case ConnectionState.none:
            case ConnectionState.waiting:
            return CustomLoaddingDialog(
                height: 150.0, width: 150, title: '努力加载中');
            break;
            case ConnectionState.done: // -------- 完成态
              if (snapshot.hasError) { // 异常
                print(snapshot.error);
                return ErrorPage(error:snapshot.error.toString());
              }else{// 正常
                if (snapshot.data['code'] == 200) {
                  var result = snapshot.data['result'];
                  if(result.isEmpty){
                    return EmptyPage();
                  }
                  return RefreshIndicator(
                      displacement: 20,
                      strokeWidth: 4,
                      color: Colors.red,
                      onRefresh: getHomeData,
                      child: SingleChildScrollView(
                        physics: BouncingScrollPhysics(),
                        child: ListView(
                          physics: ScrollPhysics(),
                          shrinkWrap: true,
                          children: <Widget>[
                            Offstage( //轮播图
                              offstage: result['routine_home_banner'] == null,
                              child: HomeBanner(homeBanner: result['routine_home_banner']),
                            ),
                            Offstage(//gridvIew导航栏
                              offstage: result['routine_home_menus'] == null,
                              child: HomeMenus(homeMenus: result['routine_home_menus']),
                            ),
                            Offstage(//新闻简报
                              offstage: result['routine_home_roll_news'] == null,
                              child: HomeRollNews(rollnews: result['routine_home_roll_news']),
                            ),
                            Offstage( //活动专题
                              offstage: result['routine_home_activity'] == null,
                              child:
                              HomeActivity(homeActivity: result['routine_home_activity']),
                            ),
                            Offstage( //快速选择
                              offstage: result['fast_product']['fast_info'] == null,
                              child: HomeTitle(title: '快速选择', subtitle: result['fast_product']['fast_info']),
                            ),
                            Offstage(
                              offstage: result['fast_product']['fast_list'] == null,
                              child: FastProduct(fastProduct: result['fast_product']['fast_list']),
                            ),
                            Offstage(//精品推荐
                              offstage: result['best_product']['best_info'] == null,
                              child: HomeTitle(title: '精品推荐', subtitle: result['best_product']['best_info']),
                            ),
                            Offstage(//精品推荐
                              offstage: result['best_product']['best_list'] == null,
                              child: BestList(bestProduct: result['best_product']['best_list']),
                            ),
                            Offstage(//首发新品
                              offstage: result['new_product']['new_info'] == null,
                              child: HomeTitle(title: '首发新品', subtitle: result['new_product']['new_info']),
                            ),
                            Offstage(//首发新品
                              offstage: result['new_product']['new_list'] == null,
                              child: NewProduct(newProduct:result['new_product']['new_list']),
                            ),
                            Offstage(
                              offstage: result['promotion_product']['promotion_info'] == null,
                              child:
                              HomeTitle(title: '促销单品', subtitle: result['promotion_product']['promotion_info']),
                            ),
                            Offstage(//促销单品
                              offstage: result['promotion_product']['promotion_list'] == null,
                              child: PromotionProduct(
                                  promotionProduct:result['promotion_product']['promotion_list']),
                            ),
                          ],
                        ),
                      ));
                }else if(snapshot.data['code']!=200){
                  return ErrorPage(error:snapshot.data['msg']);
                }
              }
              break;
            default:
              break;
          }
        },
      ),
    );

  }

  @override
  bool get wantKeepAlive => true;
}
